// pages/Protocol/ReplenishProtocol/ReplenishProtocol.js
import { updateUserIouOther , queryUserIou } from '../../../api/protocol';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    protocolId: '',
    borrowUserAddress: '', // 借款人地址
    borrowUserIdCardFrontImgUrl: '', // 借款人身份证正面
    borrowUserIdCardBackImgUrl: '', // 借款人身份证反面
    iouContent: '', // 出借人证件
    iouContentList: [], // 出借人证件列表
    otherPromises: '', // 其他约定
    otherImgUrl: '', // 其他约定图片
    otherImgUrlList: [], // 其他约定图片列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.id){
      console.log(options.id)
      var id = JSON.parse(options.id)
      console.log(id,'item');
      this.setData({protocolId: id});
      this.getDetail();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    let userId = wx.getStorageSync('userId')
    return {
      path: '/pages/Welcome/Welcome?scene='+userId,
    }
  },
  updateValue(event) {
    // event.detail 为当前输入的值
    const name = event.detail.name;
    const datas = event.detail.data;
    // const name = event.target.dataset.name||event.currentTarget.dataset.name;
    var data = this.data;
    data[name] =  datas;
    this.setData(data);
  },
  // 更新图片列表数据
  updateImgList(event){
    const name = event.detail.name;
    const datas = event.detail.data;
    const index = event.detail.index;
    var data = this.data;
    var list = [];
    console.log(data,'data');
    console.log(data[name],'data[name]')
    list = data[name];
    console.log(list,'list')
    // data[name] = datas;
    if(index === undefined){
      list.push(datas);
    }else{
      list.splice(index,1);
    }
    data[name] = list;
    this.setData(data);
  },
  getDetail(){
    let _this = this;
    queryUserIou({protocolId: _this.data.protocolId})
    .then(res => {
      if(res.success){
        var element = res.data.content[0];
        if(element.otherImgUrl){
          element.otherImgUrlArr = element.otherImgUrl ? element.otherImgUrl.split(',') : [];
        }
        _this.setData({
          borrowUserAddress: element.borrowUserAddress,
          borrowUserIdCardBackImgUrl: element.borrowUserIdCardBackImgUrl,
          borrowUserIdCardFrontImgUrl: element.borrowUserIdCardFrontImgUrl,
          otherPromises: element.otherPromises,
          otherImgUrlList: element.otherImgUrlArr,
          iouContentList: element.iouContent ? element.iouContent.split(',') : [],
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  _updateUserIouOther(){
    let _this = this;
    const { borrowUserAddress , borrowUserIdCardFrontImgUrl , borrowUserIdCardBackImgUrl , iouContentList , otherPromises , otherImgUrlList , protocolId } = _this.data;
    console.log(borrowUserIdCardFrontImgUrl,'borrowUserIdCardFrontImgUrl');
    console.log(otherImgUrlList,'otherImgUrlList');
    updateUserIouOther({id: protocolId,borrowUserAddress: borrowUserAddress,borrowUserIdCardBackImgUrl: borrowUserIdCardBackImgUrl,borrowUserIdCardFrontImgUrl: borrowUserIdCardFrontImgUrl,otherPromises: otherPromises,otherImgUrl: otherImgUrlList.join(','),iouContent: iouContentList.join(',')})
    .then(res => {
      if(res.success){
        wx.navigateBack();
        wx.showToast({
          title: '补充成功',
          icon: 'success',
        })
        const eventChannel = _this.getOpenerEventChannel()
        eventChannel.emit('updateContact',);
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
})